package shared.util;

import frontend.core.TimeTrackingWebSession;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.WebPage;

public class UIAuthUtil {
	
	public static boolean isUserAuthorizedForPage(Class<? extends WebPage> clazz) {
		boolean isAuthorized = false;
		
		if (clazz.isAnnotationPresent(AuthorizeInstantiation.class)) {
		  
			TimeTrackingWebSession webSession = TimeTrackingWebSession.getSession();
			Roles roles = webSession.getRoles();
			
			if (roles != null) {
				AuthorizeInstantiation annotation = clazz.getAnnotation(AuthorizeInstantiation.class);
				
				for (String role : roles) {
					for (int i = 0; i < annotation.value().length; i ++) {
						if (annotation.value()[i].equalsIgnoreCase(role)) {
							isAuthorized = true;
							break;
						}
					}
				}
			}
		} else {
			isAuthorized = true;
		}
		
		return isAuthorized;
	}

}
